﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Windows.Data;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;

namespace SYD_COPY_FILE
{
    public partial class Form1 : Form
    {
        #region define
        public struct _OFFECT_
        {
            public int x;
            public int y;
            public int out_w;
            public int out_h;
            public int in_w;
            public int in_h;
            public int in_R_w;
            public int in_R_h;
            public int num;
            public int x1;
            public int y1;
        };

        private static bool pictureBox_interface_ismin = true;
        private static bool SC_MAX = false;
        //Arithmetic.dll
        [System.Runtime.InteropServices.DllImport("Arithmetic.dll", EntryPoint = "crc8_fun", CallingConvention = CallingConvention.Cdecl)]
        private static extern byte crc8_fun(byte[] buf, UInt32 size);
        [System.Runtime.InteropServices.DllImport("Arithmetic.dll", EntryPoint = "crc32_fun", CallingConvention = CallingConvention.Cdecl)]
        private static extern UInt32 crc32_fun(byte[] buf, UInt32 size);
        [System.Runtime.InteropServices.DllImport("Arithmetic.dll", EntryPoint = "Dll_log_read", CallingConvention = CallingConvention.Cdecl)]
        private static extern int Dll_log_read(byte[] buf);
        [System.Runtime.InteropServices.DllImport("Arithmetic.dll", EntryPoint = "bmp_to_rbw", CallingConvention = CallingConvention.Cdecl)] 
        private static extern UInt32 bmp_to_rbw(byte[] Intputfilename, UInt32 Intputfilesize, byte[] Outfilename, UInt32 Outfilesize, byte rotation, byte ext_opt, byte extract_mode);
        [System.Runtime.InteropServices.DllImport("Arithmetic.dll", EntryPoint = "bmp_to_BlackWhite", CallingConvention = CallingConvention.Cdecl)]
        private static extern UInt32 bmp_to_BlackWhite(byte[] Intputfilename, UInt32 Intputfilesize, byte[] Outfilename, UInt32 Outfilesize, byte rotation, byte ext_opt);
        [System.Runtime.InteropServices.DllImport("Arithmetic.dll", EntryPoint = "bmp_to_8Color", CallingConvention = CallingConvention.Cdecl)]
        private static extern UInt32 bmp_to_8Color(byte[] Intputfilename, UInt32 Intputfilesize, byte[] Outfilename, UInt32 Outfilesize, byte rotation, byte ext_opt);
        [System.Runtime.InteropServices.DllImport("Arithmetic.dll", EntryPoint = "big_bin_handle", CallingConvention = CallingConvention.Cdecl)]
        private static extern UInt32 big_bin_handle(byte[] Intputfilename, UInt32 Intputfilesize, byte[] Outfilename, UInt32 Outfilesize, byte funtion, byte parm1, byte parm2);

        timestamp_accuracy_type accuracy = timestamp_accuracy_type.accuracy_3;

        public bool extend_bit_mask_state = false, timer_cal_mask_state = false;
        public byte care_bit_index = 0, care_bit_high = 15, care_bit_low = 0, care_bit_high_two = 31, care_bit_low_two = 16;
        #endregion

        public Form1()
        {
            //AutoScaleMode = AutoScaleMode.Dpi; //添加这句,要在'InitializeComponent();'上方
            InitializeComponent();

            copy_file_init();
            syd_arr_init();
            PicSplit_init();
            ui_init();
            UIgen_init();
            cos_init();

            pictureBox_interface_ismin = Settings1.Default.pictureBox_interface_ismin;

            AdjustComboBoxDropDownListWidth(comboBox_piecwise_mode);

            comboBox4.SelectedIndex = 3;
            comboBox3.SelectedIndex = 3;
            comboBox1.SelectedIndex = 1;
            comboBox2.SelectedIndex = 2;
            comboBox6.SelectedIndex = 2;
            comboBox8.SelectedIndex = 1;
            comboBox5.SelectedIndex = 0;
            comboBox10.SelectedIndex = 2;
            comboBox13.SelectedIndex = 3;
            comboBox11.SelectedIndex = 2;
            comboBox14.SelectedIndex = 3;
            comboBox9.SelectedIndex = 2;
            comboBox12.SelectedIndex = 3;
            timestamp_Difference_select.SelectedIndex = 0;
            TabCrontrol.SelectedIndex = 5;
            comboBox_piecwise_mode.SelectedIndex = 1;
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            copy_file_uninit();
            UIgen_uninit();
            Settings1.Default.Save();
        }
        const int WM_SYSCOMMAND = 0x112;
        const int SC_CLOSE = 0xF060;
        const int SC_MINIMIZE = 0xF020;
        const int SC_MAXIMIZE = 0xF030;
        const int SC_RESTORE = 61728;
        //窗体按钮的拦截函数
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_SYSCOMMAND)
            {
                if (m.WParam.ToInt32() == SC_RESTORE)
                {
                    //log.Info("窗口还原！");
                }
                if (m.WParam.ToInt32() == SC_MINIMIZE)  //拦截最小化按钮
                {
                    //这里写操作代码
                    //log.Info("点击最小化按钮！");
                }
                if (m.WParam.ToInt32() == SC_MAXIMIZE)   //拦截窗体最大化按钮
                {
                    //log.Info("点击最大化按钮！");
                    SC_MAX = true;
                }
                if (m.WParam.ToInt32() == SC_CLOSE)       //拦截窗体关闭按钮
                {
                    //log.Info("点击窗口关闭按钮！");
                }
            }
            base.WndProc(ref m);
        }
        /// <summary>
        /// 计算字符串解析表达式 1+2(2*(3+4))
        /// </summary>
        /// <param name="str">传入的字符串</param>
        /// <returns>计算得到的结果</returns>
        public decimal CalcStr(string str)
        {
            decimal num = 0m;
            //数字集合
            List<decimal> numList = new List<decimal>();
            //操作符集合
            List<Operation> operList = new List<Operation>();
            string strNum = "";
            for (int i = 0; i < str.Length; i++)
            {
                char c = str[i];

                //判断如果是数字和.
                if ((47 < c && c < 58) || c == '.')
                {
                    strNum += c;

                    if (i == str.Length - 1)
                    {
                        if (!string.IsNullOrEmpty(strNum))
                        {
                            decimal.TryParse(strNum, out num);
                            numList.Add(num);
                            strNum = "";
                        }
                    }
                    continue;
                }
                else if (c == '(')
                {
                    int temp = 1;
                    for (int j = i + 1; j < str.Length; j++)
                    {
                        var k = str[j];
                        if (k == '(')
                        {
                            temp++;
                        }
                        else if (k == ')')
                        {
                            temp--;
                        }

                        if (temp == 0)
                        {
                            temp = j - i - 1;
                        }
                    }

                    strNum = str.Substring(i + 1, temp);
                    numList.Add(CalcStr(strNum));
                    strNum = "";
                    i += temp + 1;
                }
                else
                {
                    if (!string.IsNullOrEmpty(strNum))
                    {
                        decimal.TryParse(strNum, out num);
                        numList.Add(num);
                        strNum = "";
                    }

                    if (c == '+')
                    {
                        operList.Add(new AddOperation());
                    }
                    else if (c == '-')
                    {
                        operList.Add(new SubOperation());
                    }
                    else if (c == '*')
                    {
                        operList.Add(new MultipOperation());
                    }
                    else if (c == '/')
                    {
                        operList.Add(new DivOperation());
                    }
                    else if (c == '%')
                    {
                        operList.Add(new ModOperation());
                    }
                    else
                    {
                        operList.Add(null);
                    }
                }
            }

            List<int> tempOrder = new List<int>();
            operList.ForEach(w =>
            {
                if (!tempOrder.Contains(w.PrioRity))
                {
                    tempOrder.Add(w.PrioRity);
                }

            });

            tempOrder.Sort();
            for (int t = 0; t < tempOrder.Count; t++)
            {
                for (int i = 0; i < operList.Count; i++)
                {
                    if (operList[i].PrioRity == tempOrder[t])
                    {
                        numList[i] = operList[i].OperationResult(numList[i], numList[i + 1]);
                        numList.RemoveAt(i + 1);
                        operList.RemoveAt(i);
                        i--;
                    }
                }
            }

            if (numList.Count == 1) return numList[0];

            return 0m;
        }

        public class Operation
        {
            protected int priority = 99;
            /// <summary>
            /// 优先级
            /// </summary>
            public virtual int PrioRity
            {
                get
                {
                    return priority;
                }
                set
                {
                    priority = value;
                }
            }

            public virtual decimal OperationResult(decimal a, decimal b)
            {
                return 0m;
            }
        }

        public class AddOperation : Operation
        {
            public override decimal OperationResult(decimal a, decimal b)
            {
                return a + b;
            }
        }

        public class SubOperation : Operation
        {
            public override decimal OperationResult(decimal a, decimal b)
            {
                return a - b;
            }
        }

        public class MultipOperation : Operation
        {
            public override int PrioRity
            {
                get
                {
                    return 98;
                }
            }

            public override decimal OperationResult(decimal a, decimal b)
            {
                return a * b;
            }
        }

        public class DivOperation : Operation
        {
            public override int PrioRity
            {
                get
                {
                    return 98;
                }
            }
            public override decimal OperationResult(decimal a, decimal b)
            {
                return a / b;
            }
        }

        public class ModOperation : Operation
        {
            public override int PrioRity
            {
                get
                {
                    return 97;
                }
            }
            public override decimal OperationResult(decimal a, decimal b)
            {
                return a % b;
            }
        }
        private float cal_do(ComboBox funtion, float num, float num1)
        {
            switch (funtion.SelectedIndex)
            {
                case 0:
                    num = (float)num + (float)num1;
                    break;
                case 1:
                    num = (float)num - (float)num1;
                    break;
                case 2:
                    num = (float)num * (float)num1;
                    break;
                case 3:
                    num = (float)num / (float)num1;
                    break;
                case 4:
                    num = (float)num % (float)num1;
                    break;
                case 5:
                    num = (UInt32)num & (UInt32)num1;
                    break;
                case 6:
                    num = (UInt32)num | (UInt32)num1;
                    break;
                case 7:
                    num = (UInt32)num ^ (UInt32)num1;
                    break;
            }
            return num;
        }
        private void cal_32768_subtract(string a, string b, string multiplier, bool ishex, TextBox textBox_out, TextBox textBox_hex, ComboBox comboBox_First, ComboBox comboBox_second, TextBox textBox_step2A, TextBox textBox_step2B, ComboBox comboBox_step2A, ComboBox comboBox_step2B, TextBox textBox_step2out, bool step2ishex, TextBox textBox_step2hex)
        {
            float result = 0, num = 0, num1 = 0;
            if ((a.Length == 0) | (b.Length == 0))
            {
                MessageBox.Show("input error");
                return;
            }
            if (multiplier.Length == 0)
            {
                multiplier = "1";
            }
            else
            {
                multiplier = multiplier.Replace(" ", "");
            }
            if (ishex==true)
            {
                num =(float)(Convert.ToInt32(a, 16));
                num1=(float)(Convert.ToInt32(b, 16));
            }
            else
            {
                num = Convert.ToSingle(a);
                num1 = Convert.ToSingle(b);
            }
            num = cal_do(comboBox_First, (float)num, (float)num1);
            
            result = (float)CalcStr(multiplier);
            result = cal_do(comboBox_second, (float)num, (float)result);

            textBox_out.Text = result.ToString();
            textBox_hex.Text = "0x" + ((UInt32)result).ToString("X");
            if (textBox_step2A.Text.Length != 0)
            {
                if (step2ishex == true) num = (float)(Convert.ToInt32(textBox_step2A.Text, 16));
                else num = Convert.ToSingle(textBox_step2A.Text);
                result = cal_do(comboBox_step2A, (float)result, (float)num);
                if (textBox_step2B.Text.Length != 0)
                {
                    if (step2ishex == true) num = (float)(Convert.ToInt32(textBox_step2B.Text, 16));
                    else num = Convert.ToSingle(textBox_step2B.Text);
                    result = cal_do(comboBox_step2B, (float)result, (float)num);
                }
                textBox_step2out.Text = result.ToString();
                textBox_step2hex.Text = "0x" + ((UInt32)result).ToString("X");
            }
        }
        enum timestamp_accuracy_type
        {
            accuracy_3,//三位精度
            accuracy_6,
            Display_DATA,//显示年月日
            CONVERT_UTC8,//转换为北京时间
            CONVERT_UTC,//转换为UTC时间
        }
        //timestamp要显示的时间戳，单位：ms 一般是差值
        private string cal_Calendar_time_difference_subtract_output(UInt64 timestamp, timestamp_accuracy_type accuracy, TextBox testbox_display, TextBox testbox_display_timestamp)
        {
            Int32 day = 0, millisecond, second, minute, hour, microsecond = 0;
            string result = "";
            if (accuracy == timestamp_accuracy_type.accuracy_6)
            {
                microsecond = (Int32)(timestamp % 1000);
                timestamp = timestamp / 1000;
            }

            UInt64 timestamp1 = timestamp / 1000;

            millisecond = (Int32)(timestamp % 1000);
            timestamp = timestamp / 1000;
            second = (Int32)(timestamp % 60);
            timestamp = timestamp / 60;
            minute = (Int32)(timestamp % 60);
            timestamp = timestamp / 60;
            hour = (Int32)(timestamp % 24);
            timestamp = timestamp / 24;
            //day = (Int32)(timestamp % 31);
            day = (Int32)timestamp;

            if (day == 0)
                result = hour.ToString("D2") + ":" + minute.ToString("D2") + ":" + second.ToString("D2") + ":" + millisecond.ToString("D3");
            else
                result = day.ToString("D2") + " " + hour.ToString("D2") + ":" + minute.ToString("D2") + ":" + second.ToString("D2") + ":" + millisecond.ToString("D3");

            if (accuracy == timestamp_accuracy_type.accuracy_6)
            {
                result += microsecond.ToString("D3");
            }

            if (testbox_display != null)
                testbox_display.Text = result;

            if (testbox_display_timestamp != null)
                testbox_display_timestamp.Text = "0x" + timestamp1.ToString("x");

            return result;
        }
        private string cal_Calendar_time_difference_subtract(string a, string b, int c, TextBox testbox_display, TextBox testbox_display_timestamp)
        {
            UInt64 timestamp = 0, timestamp1 = 0;
            if ((a.Length == 0) | (b.Length == 0))
            {
                MessageBox.Show("input error");
                return null;
            }
            timestamp = cal_Calendar_srt_to_timestamp(a);

            timestamp = (UInt32)c * 86400000 + timestamp;

            timestamp1 = cal_Calendar_srt_to_timestamp(b);
            if (timestamp1 > timestamp)
            {
                timestamp = 86400000 + timestamp;
                MessageBox.Show("发现隔天数据");
            }
            timestamp = timestamp - timestamp1;
            return cal_Calendar_time_difference_subtract_output(timestamp, timestamp_accuracy_type.accuracy_3, testbox_display, testbox_display_timestamp);
        }
        private string time_difference_subtract(string a, string b, int c, TextBox testbox_display, TextBox testbox_display_timestamp)
        {
            if ((a.Length == 17)&& (a.Contains(":") == true))//20250707_15:43:32
            {
                a = a.Replace(":", "");
            }
            if ((b.Length == 17) && (b.Contains(":") == true))//20250707_15:43:32
            {
                b = b.Replace(":", "");
            }
            if (a.Length!=b.Length)
            {
                MessageBox.Show("two input length are different.");
                return "";
            }
            if (a.Length > 13)
            {
                if (a.Contains(":") == true)
                    return cal_Calendar_time_differenceble_subtract(a, b, testbox_display, testbox_display_timestamp);//22:52:27.252726
                else
                    return cal_Calendar_day_differenceble_subtract(a, b, testbox_display, testbox_display_timestamp);//20220223 124748
            }
            else
            {
                return cal_Calendar_time_difference_subtract(a, b, c, testbox_display, testbox_display_timestamp);//18:05:59:505
            }
        }
        private UInt64 cal_Calendar_srt_to_timestamp(string a)
        {
            Int32 hour = 0, minute = 0, second = 0, millisecond = 0;
            UInt64 timestamp = 0;

            hour = Convert.ToInt32(a.Substring(0, 2), 10);
            minute = Convert.ToInt32(a.Substring(3, 2), 10);
            second = Convert.ToInt32(a.Substring(6, 2), 10);
            millisecond = Convert.ToInt32(a.Substring(9, 3), 10);
            timestamp = ((UInt64)hour * 3600 + (UInt64)minute * 60 + (UInt64)second) * 1000 + (UInt64)millisecond;

            return timestamp;
        }

        private string cal_Calendar_time_differenceble_subtract(string a, string b, TextBox testbox_display, TextBox testbox_display_timestamp)
        {
            Int32 hour = 0, minute = 0, second = 0, millisecond = 0, microsecond = 0, hour1 = 0, minute1 = 0, second1 = 0, millisecond1 = 0, microsecond1 = 0;
            UInt64 timestamp = 0, timestamp1 = 0;

            if ((a.Length == 0) | (b.Length == 0))
            {
                MessageBox.Show("input error");
                return null;
            }
            List<string> lstArray = a.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).ToList();
            if (lstArray.Count > 1)
            {
                a = lstArray[1];
            }
            lstArray = b.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).ToList();
            if (lstArray.Count > 1)
            {
                b = lstArray[1];
            }
            bool hour_half_byte = false;
            try
            {
                hour = Convert.ToInt32(a.Substring(0, 2), 10);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                hour_half_byte = true;
            }
            if(hour_half_byte)
            {
                hour = Convert.ToInt32(a.Substring(0, 1), 10);
                minute = Convert.ToInt32(a.Substring(2, 2), 10);
                second = Convert.ToInt32(a.Substring(5, 2), 10);
                millisecond = Convert.ToInt32(a.Substring(8, 3), 10);
                microsecond = Convert.ToInt32(a.Substring(11, 3), 10);
            }
            else
            {
                hour = Convert.ToInt32(a.Substring(0, 2), 10);
                minute = Convert.ToInt32(a.Substring(3, 2), 10);
                second = Convert.ToInt32(a.Substring(6, 2), 10);
                millisecond = Convert.ToInt32(a.Substring(9, 3), 10);
                microsecond = Convert.ToInt32(a.Substring(12, 3), 10);
            }
            timestamp = ((UInt64)hour * 3600 + (UInt64)minute * 60 + (UInt64)second) * 1000 * 1000 + (UInt64)millisecond * 1000 + (UInt64)microsecond;

            if (hour_half_byte)
            {
                hour1 = Convert.ToInt32(b.Substring(0, 1), 10);
                minute1 = Convert.ToInt32(b.Substring(2, 2), 10);
                second1 = Convert.ToInt32(b.Substring(5, 2), 10);
                millisecond1 = Convert.ToInt32(b.Substring(8, 3), 10);
                microsecond1 = Convert.ToInt32(b.Substring(11, 3), 10);
            }
            else
            {
                hour1 = Convert.ToInt32(b.Substring(0, 2), 10);
                minute1 = Convert.ToInt32(b.Substring(3, 2), 10);
                second1 = Convert.ToInt32(b.Substring(6, 2), 10);
                millisecond1 = Convert.ToInt32(b.Substring(9, 3), 10);
                microsecond1 = Convert.ToInt32(b.Substring(12, 3), 10);
            }
            timestamp1 = ((UInt64)hour1 * 3600 + (UInt64)minute1 * 60 + (UInt64)second1) * 1000 * 1000 + (UInt64)millisecond1 * 1000 + (UInt64)microsecond1;

            timestamp = timestamp - timestamp1;

            return cal_Calendar_time_difference_subtract_output(timestamp, timestamp_accuracy_type.accuracy_6, testbox_display, testbox_display_timestamp);
        }
        private string cal_Calendar_day_differenceble_subtract(string a, string b, TextBox testbox_display, TextBox testbox_display_timestamp)
        {
            if (a.Contains("_")) a=a.Replace("_"," ");
            if (b.Contains("_")) b=b.Replace("_", " ");
            DateTime Now_Time = DateTime.ParseExact(a, "yyyyMMdd HHmmss", new CultureInfo("fr-FR"));
            DateTime Before_Time = DateTime.ParseExact(b, "yyyyMMdd HHmmss", new CultureInfo("fr-FR"));
            TimeSpan ts = Now_Time.Subtract(Before_Time);

            return cal_Calendar_time_difference_subtract_output((UInt64)(ts.TotalMilliseconds), timestamp_accuracy_type.accuracy_3, testbox_display, testbox_display_timestamp);
        }

        private string cal_Calendar_ble_time_sum(string a, string b, TextBox testbox_display, TextBox testbox_display_timestamp)
        {
            Int32 hour = 0, minute = 0, second = 0, millisecond = 0, microsecond = 0, hour1 = 0, minute1 = 0, second1 = 0, millisecond1 = 0, microsecond1 = 0;
            UInt64 timestamp = 0, timestamp1 = 0;
            string result = "";

            if ((a.Length == 0) | (b.Length == 0))
            {
                MessageBox.Show("input error");
                return null;
            }
            List<string> lstArray = a.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).ToList();
            if (lstArray.Count > 1)
            {
                a = lstArray[1];
            }
            lstArray = b.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).ToList();
            if (lstArray.Count > 1)
            {
                b = lstArray[1];
            }
            hour = Convert.ToInt32(a.Substring(0, 2), 10);
            minute = Convert.ToInt32(a.Substring(3, 2), 10);
            second = Convert.ToInt32(a.Substring(6, 2), 10);
            millisecond = Convert.ToInt32(a.Substring(9, 3), 10);
            microsecond = Convert.ToInt32(a.Substring(12, 3), 10);
            timestamp = ((UInt64)hour * 3600 + (UInt64)minute * 60 + (UInt64)second) * 1000 * 1000 + (UInt64)millisecond * 1000 + (UInt64)microsecond;

            hour1 = Convert.ToInt32(b.Substring(0, 2), 10);
            minute1 = Convert.ToInt32(b.Substring(3, 2), 10);
            second1 = Convert.ToInt32(b.Substring(6, 2), 10);
            millisecond1 = Convert.ToInt32(b.Substring(9, 3), 10);
            microsecond1 = Convert.ToInt32(b.Substring(12, 3), 10);
            timestamp1 = ((UInt64)hour1 * 3600 + (UInt64)minute1 * 60 + (UInt64)second1) * 1000 * 1000 + (UInt64)millisecond1 * 1000 + (UInt64)microsecond1;

            timestamp = timestamp + timestamp1;
            timestamp1 = timestamp / 1000;

            microsecond = (Int32)(timestamp % 1000);
            timestamp = timestamp / 1000;
            millisecond = (Int32)(timestamp % 1000);
            timestamp = timestamp / 1000;
            second = (Int32)(timestamp % 60);
            timestamp = timestamp / 60;
            minute = (Int32)(timestamp % 60);
            timestamp = timestamp / 60;
            hour = (Int32)(timestamp % 24);

            result = hour.ToString("D2") + ":" + minute.ToString("D2") + ":" + second.ToString("D2") + ":" + millisecond.ToString("D3") + microsecond.ToString("D3");
            if (testbox_display != null)
                testbox_display.Text = result;

            if (testbox_display_timestamp != null)
                testbox_display_timestamp.Text = "0x" + timestamp1.ToString("x");

            return result;
        }
        private void cal_timestamp_difference_subtract(string a, string b, timestamp_accuracy_type accuracy, TextBox testbox_display)
        {
            Int32 hour = 0, minute = 0, second = 0, millisecond = 0, day = 0, mouth = 0, year = 0;
            UInt32 timestamp = 0, timestamp1 = 0;
            string result = "";
            if ((a.Length == 0) | (b.Length == 0))
            {
                MessageBox.Show("input error");
                return;
            }
            if (timestamp_Difference_hex.Checked == false)//十进制
                timestamp = Convert.ToUInt32(a);
            else
                timestamp = Convert.ToUInt32(a.Substring(2, 8), 16);
            if ((accuracy == timestamp_accuracy_type.CONVERT_UTC8) || (accuracy == timestamp_accuracy_type.CONVERT_UTC))
            {
                System.DateTime startTime;
                if (accuracy == timestamp_accuracy_type.CONVERT_UTC8)
                    startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));//当地时区
                else
                    startTime = TimeZone.CurrentTimeZone.ToUniversalTime(new System.DateTime(1970, 1, 1));//UTC时区

                DateTime time = startTime.AddSeconds(timestamp);
                result = time.ToString("yyyy-MM-dd HHmmss");
            }
            else
            {
                if (timestamp_Difference_hex.Checked == false)//十进制
                    timestamp1 = Convert.ToUInt32(b);
                else
                    timestamp1 = Convert.ToUInt32(b.Substring(2, 8), 16);
                timestamp = timestamp - timestamp1;

                if (accuracy == timestamp_accuracy_type.accuracy_6)
                {
                    millisecond = (Int32)(timestamp % 1000);
                    timestamp = timestamp / 1000;
                }

                second = (Int32)(timestamp % 60);
                timestamp = timestamp / 60;
                minute = (Int32)(timestamp % 60);
                timestamp = timestamp / 60;
                hour = (Int32)(timestamp % 24);
                if (accuracy == timestamp_accuracy_type.Display_DATA)
                {
                    timestamp = timestamp / 24;
                    day = (Int32)(timestamp % 30);
                    timestamp = timestamp / 30;
                    mouth = (Int32)(timestamp % 12);
                    year = (Int32)(timestamp / 12);
                    result = year.ToString("D4") + "-" + mouth.ToString("D2") + "-" + day.ToString("D2") + " ";
                }

                result += hour.ToString("D2") + ":" + minute.ToString("D2") + ":" + second.ToString("D2");
                if (accuracy == timestamp_accuracy_type.accuracy_6)
                {
                    result += ":" + millisecond.ToString("D3");
                }
            }

            testbox_display.Text = result;
        }

        private _OFFECT_ cal_offect_subtract(_OFFECT_ offect)
        {
            int x = 0, y = 0;
            x = offect.out_w - offect.x * offect.num;
            y = offect.out_h - offect.y;
            offect.in_w = x / 2;
            offect.in_h = y / 2;
            if (x % 2 == 1)
            {
                offect.in_R_w = x / 2 + 1;
            }
            else
            {
                offect.in_R_w = x / 2;
            }
            offect.in_R_h = offect.out_h - offect.in_h - offect.y;
            offect.x1 = offect.out_w - offect.in_R_w;
            offect.y1 = offect.out_h - offect.in_R_h;
            return offect;
        }

        // 将文件转换为byte数组
        //param name="path"文件地址
        //返回值 转换后的byte数组
        public static byte[] File2Bytes(string path)
        {
            if (Path.GetExtension(path) == ".hex")//如果传入的hex文件就到同目录去找bin文件再倒入
                path = path.Replace(".hex", ".bin");

            if (!System.IO.File.Exists(path))
            {
                return new byte[0];
            }

            FileInfo fi = new FileInfo(path);
            byte[] buff = new byte[fi.Length];

            FileStream fs = fi.OpenRead();
            fs.Read(buff, 0, Convert.ToInt32(fs.Length));
            fs.Close();

            return buff;
        }
        private void pictureBoxinterface_Doing(bool min)
        {
            if (min == true)
            {
                pictureBox_interface_ismin = false;
                this.Width = 612;
                this.Height = 232 + 22;
            }
            else
            {
                pictureBox_interface_ismin = true;
                this.Width = 782;
                this.Height = 620 + 22;
            }
            extend_synccopyfile_state = false;
        }

        private void pictureBoxinterface_Click(object sender, EventArgs e)
        {
            if (pictureBox_interface_ismin == true)
            {
                pictureBoxinterface_Doing(true);
            }
            else
            {
                pictureBoxinterface_Doing(false);
            }
            this.WindowState = FormWindowState.Normal;
        }

        private void cal_32768_Click(object sender, EventArgs e)
        {
            if (sender == button2)
                cal_32768_subtract(textBox6.Text, textBox8.Text, textBox1.Text, checkBox8.Checked, textBox7, textBox10, comboBox2, comboBox3, textBox49, textBox52, comboBox10, comboBox13, textBox55, checkBox19.Checked,textBox20);
            else if (sender == button8)
                cal_32768_subtract(textBox43.Text, textBox45.Text, textBox11.Text, checkBox9.Checked, textBox44, textBox12, comboBox8, comboBox5, textBox47, textBox51, comboBox9, comboBox12, textBox59, checkBox20.Checked, textBox18);
            else
                cal_32768_subtract(textBox3.Text, textBox5.Text, textBox2.Text, checkBox6.Checked, textBox4, textBox9, comboBox1, comboBox4, textBox50, textBox53, comboBox11, comboBox14, textBox58, checkBox18.Checked, textBox19);
        }
        private bool cal_32768_check(string a, string b, string c, ComboBox aa, ComboBox bb)
        {
            if ((a.Length == 0) | (b.Length == 0) | (c.Length == 0)) return false;
            if ((aa.SelectedIndex == 3) && (Convert.ToInt32(b, 16) == 0)) return false;
            if ((bb.SelectedIndex == 3) && (Convert.ToInt32(c, 16) == 0)) return false;
            return true;
        }
        private void cal_32768_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if ((sender == textBox8) || (sender == textBox1) || (sender == textBox6))
                {
                    if (cal_32768_check(textBox6.Text, textBox8.Text, textBox1.Text, comboBox2, comboBox3))
                        cal_32768_subtract(textBox6.Text, textBox8.Text, textBox1.Text, checkBox8.Checked, textBox7, textBox10, comboBox2, comboBox3, textBox49, textBox52, comboBox10, comboBox13, textBox55, checkBox19.Checked, textBox20);
                }
                else if ((sender == textBox45) || (sender == textBox11) || (sender == textBox43))
                {
                    if (cal_32768_check(textBox43.Text, textBox45.Text, textBox11.Text, comboBox8, comboBox5))
                        cal_32768_subtract(textBox43.Text, textBox45.Text, textBox11.Text, checkBox9.Checked, textBox44, textBox12, comboBox8, comboBox5, textBox47, textBox51, comboBox9, comboBox12, textBox59, checkBox20.Checked, textBox18);
                }
                else
                {
                    if (cal_32768_check(textBox3.Text, textBox5.Text, textBox2.Text, comboBox1, comboBox4))
                        cal_32768_subtract(textBox3.Text, textBox5.Text, textBox2.Text, checkBox6.Checked, textBox4, textBox9, comboBox1, comboBox4, textBox50, textBox53, comboBox11, comboBox14, textBox58, checkBox18.Checked, textBox19);
                }
            }
            catch (Exception ex) // 异常处理
            {
                Console.WriteLine(ex);
                return;
            }
        }
        private void Calendar_Time_Difference_Cal_Click(object sender, EventArgs e)
        {
            Int32 date = 0;
            TextBox Now_Time_textBox = textBox21;
            TextBox Before_Time_textBox = textBox22;
            TextBox Day_textBox = textBox25;
            TextBox Output_time_textBox = textBox23;
            TextBox Output_hexsecond_textBox = textBox56;
            if ((Button)(sender) == Calendar_Time_Difference_Cal1)
            {
                Now_Time_textBox = textBox29;
                Before_Time_textBox = textBox28;
                Day_textBox = textBox26;
                Output_time_textBox = textBox27;
                Output_hexsecond_textBox = textBox57;
            }
            else if ((Button)(sender) == Calendar_Time_Difference_Cal2)
            {
                Now_Time_textBox = textBox39;
                Before_Time_textBox = textBox38;
                Day_textBox = textBox30;
                Output_time_textBox = textBox34;
                Output_hexsecond_textBox = textBox60;
            }
            else if ((Button)(sender) == Calendar_Time_Difference_Cal3)
            {
                Now_Time_textBox = textBox17;
                Before_Time_textBox = textBox16;
                Day_textBox = textBox14;
                Output_time_textBox = textBox15;
                Output_hexsecond_textBox = textBox13;
            }

            if ((Now_Time_textBox.Text.Length == 0) || (Before_Time_textBox.Text.Length == 0))
            {
                MessageBox.Show("input error");
                return;
            }

            if (Day_textBox.Text.Length != 0)
            {
                date = Convert.ToInt32(Day_textBox.Text, 10);
            }
            time_difference_subtract(Now_Time_textBox.Text, Before_Time_textBox.Text, date, Output_time_textBox, Output_hexsecond_textBox);
        }
        void TextBox_increment(TextBox box)
        {
            Int32 day = Convert.ToInt32(box.Text, 10);
            day = day + 1;
            box.Text = day.ToString();
        }
        private void Calendar_Time_label_Click(object sender, EventArgs e)
        {
            if ((Label)(sender) == label16)
            {
                TextBox_increment(textBox25);
            }
            else if ((Label)(sender) == label17)
            {
                TextBox_increment(textBox26);
            }
            else if ((Label)(sender) == label20)
            {
                TextBox_increment(textBox30);
            }
            else if ((Label)(sender) == label9)
            {
                TextBox_increment(textBox14);
            }
        }
        private void timestamp_Difference_cal_Click(object sender, EventArgs e)
        {
            TextBox Now_timestamp_textBox = textBox37;
            TextBox Before_timestamp_textBox = textBox36;
            TextBox Output_timestamp_textBox = textBox35;

            if ((Button)(sender) == timestamp_Difference_cal1)
            {
                Now_timestamp_textBox = textBox33;
                Before_timestamp_textBox = textBox32;
                Output_timestamp_textBox = textBox31;
            }

            if ((Now_timestamp_textBox.Text.Length == 0) || (Before_timestamp_textBox.Text.Length == 0))
            {
                MessageBox.Show("input error");
                return;
            }

            cal_timestamp_difference_subtract(Now_timestamp_textBox.Text, Before_timestamp_textBox.Text, accuracy, Output_timestamp_textBox);
        }

        private void bitmask_checkbox_Click(object sender, EventArgs e)
        {
            UInt32 bit_mask = 0;
            if (BIT_MARK0.CheckState == CheckState.Checked) bit_mask |= 0x01;
            if (BIT_MARK1.CheckState == CheckState.Checked) bit_mask |= 0x02;
            if (BIT_MARK2.CheckState == CheckState.Checked) bit_mask |= 0x04;
            if (BIT_MARK3.CheckState == CheckState.Checked) bit_mask |= 0x08;
            if (BIT_MARK4.CheckState == CheckState.Checked) bit_mask |= 0x10;
            if (BIT_MARK5.CheckState == CheckState.Checked) bit_mask |= 0x20;
            if (BIT_MARK6.CheckState == CheckState.Checked) bit_mask |= 0x40;
            if (BIT_MARK7.CheckState == CheckState.Checked) bit_mask |= 0x80;
            if (BIT_MARK8.CheckState == CheckState.Checked) bit_mask |= 0x100;
            if (BIT_MARK9.CheckState == CheckState.Checked) bit_mask |= 0x200;
            if (BIT_MARK10.CheckState == CheckState.Checked) bit_mask |= 0x400;
            if (BIT_MARK11.CheckState == CheckState.Checked) bit_mask |= 0x800;
            if (BIT_MARK12.CheckState == CheckState.Checked) bit_mask |= 0x1000;
            if (BIT_MARK13.CheckState == CheckState.Checked) bit_mask |= 0x2000;
            if (BIT_MARK14.CheckState == CheckState.Checked) bit_mask |= 0x4000;
            if (BIT_MARK15.CheckState == CheckState.Checked) bit_mask |= 0x8000;
            if (BIT_MARK16.CheckState == CheckState.Checked) bit_mask |= 0x10000;
            if (BIT_MARK17.CheckState == CheckState.Checked) bit_mask |= 0x20000;
            if (BIT_MARK18.CheckState == CheckState.Checked) bit_mask |= 0x40000;
            if (BIT_MARK19.CheckState == CheckState.Checked) bit_mask |= 0x80000;
            if (BIT_MARK20.CheckState == CheckState.Checked) bit_mask |= 0x100000;
            if (BIT_MARK21.CheckState == CheckState.Checked) bit_mask |= 0x200000;
            if (BIT_MARK22.CheckState == CheckState.Checked) bit_mask |= 0x400000;
            if (BIT_MARK23.CheckState == CheckState.Checked) bit_mask |= 0x800000;
            if (BIT_MARK24.CheckState == CheckState.Checked) bit_mask |= 0x1000000;
            if (BIT_MARK25.CheckState == CheckState.Checked) bit_mask |= 0x2000000;
            if (BIT_MARK26.CheckState == CheckState.Checked) bit_mask |= 0x4000000;
            if (BIT_MARK27.CheckState == CheckState.Checked) bit_mask |= 0x8000000;
            if (BIT_MARK28.CheckState == CheckState.Checked) bit_mask |= 0x10000000;
            if (BIT_MARK29.CheckState == CheckState.Checked) bit_mask |= 0x20000000;
            if (BIT_MARK30.CheckState == CheckState.Checked) bit_mask |= 0x40000000;
            if (BIT_MARK31.CheckState == CheckState.Checked) bit_mask |= 0x80000000;
            bit_mask_result.Text = "0x" + bit_mask.ToString("X8");
        }
        private void LED_input1_textBox_keydown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string str = LED_input1_textBox.Text.Trim(), str_out = "";
                while (true)
                {
                    if (str.Length >= 4)
                    {
                        byte input = Convert.ToByte(str.Substring(2, 2), 16);
                        UInt32 output = 0;

                        byte temp = Convert.ToByte(textBox_GPIO0.Text, 10);
                        if ((input & 0x01) == 0x01)
                            output |= (UInt32)(1 << temp);

                        temp = Convert.ToByte(textBox_GPIO1.Text, 10);
                        if ((input & 0x02) == 0x02)
                            output |= (UInt32)(1 << temp);

                        temp = Convert.ToByte(textBox_GPIO2.Text, 10);
                        if ((input & 0x04) == 0x04)
                            output |= (UInt32)(1 << temp);

                        temp = Convert.ToByte(textBox_GPIO3.Text, 10);
                        if ((input & 0x08) == 0x08)
                            output |= (UInt32)(1 << temp);

                        temp = Convert.ToByte(textBox_GPIO4.Text, 10);
                        if ((input & 0x10) == 0x10)
                            output |= (UInt32)(1 << temp);

                        temp = Convert.ToByte(textBox_GPIO5.Text, 10);
                        if ((input & 0x20) == 0x20)
                            output |= (UInt32)(1 << temp);

                        temp = Convert.ToByte(textBox_GPIO6.Text, 10);
                        if ((input & 0x40) == 0x40)
                            output |= (UInt32)(1 << temp);

                        if (textBox_GPIO7.Text.Length != 0)
                        {
                            temp = Convert.ToByte(textBox_GPIO7.Text, 10);
                            if ((input & 0x80) == 0x80)
                                output |= (UInt32)(1 << temp);
                        }
                        str_out += "0x" + output.ToString("X8");

                        if (str.Length > 4)
                        {
                            str = str.Remove(0, 5);
                            if (str.Length >= 4) str_out += ",";
                        }
                        else str = "";
                    }
                    else break;
                }

                LED_result1_textBox.Text = str_out;
            }
        }

        private void bit_mask_result_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string input_str = bit_mask_result.Text.Trim().Replace("0X", "").Replace("0x", "");
                UInt32 input = Convert.ToUInt32(input_str, 16);
                if ((input & 0x01) == 0x01) BIT_MARK0.Checked = true;
                else BIT_MARK0.Checked = false;
                if ((input & 0x02) == 0x02) BIT_MARK1.Checked = true;
                else BIT_MARK1.Checked = false;
                if ((input & 0x04) == 0x04) BIT_MARK2.Checked = true;
                else BIT_MARK2.Checked = false;
                if ((input & 0x08) == 0x08) BIT_MARK3.Checked = true;
                else BIT_MARK3.Checked = false;
                if ((input & 0x10) == 0x10) BIT_MARK4.Checked = true;
                else BIT_MARK4.Checked = false;
                if ((input & 0x20) == 0x20) BIT_MARK5.Checked = true;
                else BIT_MARK5.Checked = false;
                if ((input & 0x40) == 0x40) BIT_MARK6.Checked = true;
                else BIT_MARK6.Checked = false;
                if ((input & 0x80) == 0x80) BIT_MARK7.Checked = true;
                else BIT_MARK7.Checked = false;
                if ((input & 0x100) == 0x100) BIT_MARK8.Checked = true;
                else BIT_MARK8.Checked = false;
                if ((input & 0x200) == 0x200) BIT_MARK9.Checked = true;
                else BIT_MARK9.Checked = false;
                if ((input & 0x400) == 0x400) BIT_MARK10.Checked = true;
                else BIT_MARK10.Checked = false;
                if ((input & 0x800) == 0x800) BIT_MARK11.Checked = true;
                else BIT_MARK11.Checked = false;
                if ((input & 0x1000) == 0x1000) BIT_MARK12.Checked = true;
                else BIT_MARK12.Checked = false;
                if ((input & 0x2000) == 0x2000) BIT_MARK13.Checked = true;
                else BIT_MARK13.Checked = false;
                if ((input & 0x4000) == 0x4000) BIT_MARK14.Checked = true;
                else BIT_MARK14.Checked = false;
                if ((input & 0x8000) == 0x8000) BIT_MARK15.Checked = true;
                else BIT_MARK15.Checked = false;
                if ((input & 0x10000) == 0x10000) BIT_MARK16.Checked = true;
                else BIT_MARK16.Checked = false;
                if ((input & 0x20000) == 0x20000) BIT_MARK17.Checked = true;
                else BIT_MARK17.Checked = false;
                if ((input & 0x40000) == 0x40000) BIT_MARK18.Checked = true;
                else BIT_MARK18.Checked = false;
                if ((input & 0x80000) == 0x80000) BIT_MARK19.Checked = true;
                else BIT_MARK19.Checked = false;
                if ((input & 0x100000) == 0x100000) BIT_MARK20.Checked = true;
                else BIT_MARK20.Checked = false;
                if ((input & 0x200000) == 0x200000) BIT_MARK21.Checked = true;
                else BIT_MARK21.Checked = false;
                if ((input & 0x400000) == 0x400000) BIT_MARK22.Checked = true;
                else BIT_MARK22.Checked = false;
                if ((input & 0x800000) == 0x800000) BIT_MARK23.Checked = true;
                else BIT_MARK23.Checked = false;
                if ((input & 0x1000000) == 0x1000000) BIT_MARK24.Checked = true;
                else BIT_MARK24.Checked = false;
                if ((input & 0x2000000) == 0x2000000) BIT_MARK25.Checked = true;
                else BIT_MARK25.Checked = false;
                if ((input & 0x4000000) == 0x4000000) BIT_MARK26.Checked = true;
                else BIT_MARK26.Checked = false;
                if ((input & 0x8000000) == 0x8000000) BIT_MARK27.Checked = true;
                else BIT_MARK27.Checked = false;
                if ((input & 0x10000000) == 0x10000000) BIT_MARK28.Checked = true;
                else BIT_MARK28.Checked = false;
                if ((input & 0x20000000) == 0x20000000) BIT_MARK29.Checked = true;
                else BIT_MARK29.Checked = false;
                if ((input & 0x40000000) == 0x40000000) BIT_MARK30.Checked = true;
                else BIT_MARK30.Checked = false;
                if ((input & 0x80000000) == 0x80000000) BIT_MARK31.Checked = true;
                else BIT_MARK31.Checked = false;
            }
        }
        private void bit_mask_result_TextChanged(object sender, EventArgs e)
        {
            string input_str= bit_mask_result.Text.Trim().Replace("0X","").Replace("0x", "");
            bit_dec_result.Text=HexStringToString(input_str);
            if (input_str.Length<=8)
            {
                UInt32 data = 0;
                data = Convert.ToUInt32(input_str, 16);
                data = ~data;
                bit_nomask_result.Text = "0x"+data.ToString("X8");

                data = Convert.ToUInt32(input_str, 16);
                data >>= care_bit_low;
                UInt32 bit = 0;
                for (byte i=0;i<=(care_bit_high- care_bit_low);i++)
                {
                    bit |= (UInt32)(1<<i);
                }
                UInt32 input = (UInt32)(data & bit);
                if ((input & 0x01) == 0x01) Care_BIT_MARK0.Checked = true;
                else Care_BIT_MARK0.Checked = false;
                if ((input & 0x02) == 0x02) Care_BIT_MARK1.Checked = true;
                else Care_BIT_MARK1.Checked = false;
                if ((input & 0x04) == 0x04) Care_BIT_MARK2.Checked = true;
                else Care_BIT_MARK2.Checked = false;
                if ((input & 0x08) == 0x08) Care_BIT_MARK3.Checked = true;
                else Care_BIT_MARK3.Checked = false;
                if ((input & 0x10) == 0x10) Care_BIT_MARK4.Checked = true;
                else Care_BIT_MARK4.Checked = false;
                if ((input & 0x20) == 0x20) Care_BIT_MARK5.Checked = true;
                else Care_BIT_MARK5.Checked = false;
                if ((input & 0x40) == 0x40) Care_BIT_MARK6.Checked = true;
                else Care_BIT_MARK6.Checked = false;
                if ((input & 0x80) == 0x80) Care_BIT_MARK7.Checked = true;
                else Care_BIT_MARK7.Checked = false;
                if ((input & 0x100) == 0x100) Care_BIT_MARK8.Checked = true;
                else Care_BIT_MARK8.Checked = false;
                if ((input & 0x200) == 0x200) Care_BIT_MARK9.Checked = true;
                else Care_BIT_MARK9.Checked = false;
                if ((input & 0x400) == 0x400) Care_BIT_MARK10.Checked = true;
                else Care_BIT_MARK10.Checked = false;
                if ((input & 0x800) == 0x800) Care_BIT_MARK11.Checked = true;
                else Care_BIT_MARK11.Checked = false;
                if ((input & 0x1000) == 0x1000) Care_BIT_MARK12.Checked = true;
                else Care_BIT_MARK12.Checked = false;
                if ((input & 0x2000) == 0x2000) Care_BIT_MARK13.Checked = true;
                else Care_BIT_MARK13.Checked = false;
                if ((input & 0x4000) == 0x4000) Care_BIT_MARK14.Checked = true;
                else Care_BIT_MARK14.Checked = false;
                if ((input & 0x8000) == 0x8000) Care_BIT_MARK15.Checked = true;
                else Care_BIT_MARK15.Checked = false;
                if ((input & 0x10000) == 0x10000) Care_BIT_MARK16.Checked = true;
                else Care_BIT_MARK16.Checked = false;
                if ((input & 0x20000) == 0x20000) Care_BIT_MARK17.Checked = true;
                else Care_BIT_MARK17.Checked = false;
                if ((input & 0x40000) == 0x40000) Care_BIT_MARK18.Checked = true;
                else Care_BIT_MARK18.Checked = false;
                if ((input & 0x80000) == 0x80000) Care_BIT_MARK19.Checked = true;
                else Care_BIT_MARK19.Checked = false;
                bit_mask_care_result.Text = "0x" + input.ToString("X");

                data = Convert.ToUInt32(input_str, 16);
                data <<= (31 - care_bit_high_two);
                data >>= (31 - care_bit_high_two+ care_bit_low_two);
                bit_mask_care_result_two.Text = "0x" + data.ToString("X");
                bit_dec_care_result_two.Text = data.ToString();
            }
        }
        private void bit_mask_care_result_TextChanged(object sender, EventArgs e)
        {
            string input_str = bit_mask_care_result.Text.Trim().Replace("0X", "").Replace("0x", "");
            bit_dec_care_result.Text = HexStringToString(input_str);
        }
        private void bit_nomask_result_TextChanged(object sender, EventArgs e)
        {
            string input_str = bit_nomask_result.Text.Trim().Replace("0X", "").Replace("0x", "");
            bit_nomask_dec_result.Text = HexStringToString(input_str);
        }
        private void button17_Click(object sender, EventArgs e)
        {
            BIT_MARK0.Checked = false;
            BIT_MARK1.Checked = false;
            BIT_MARK2.Checked = false;
            BIT_MARK3.Checked = false;
            BIT_MARK4.Checked = false;
            BIT_MARK5.Checked = false;
            BIT_MARK6.Checked = false;
            BIT_MARK7.Checked = false;
            BIT_MARK8.Checked = false;
            BIT_MARK9.Checked = false;
            BIT_MARK10.Checked = false;
            BIT_MARK11.Checked = false;
            BIT_MARK12.Checked = false;
            BIT_MARK13.Checked = false;
            BIT_MARK14.Checked = false;
            BIT_MARK15.Checked = false;
            BIT_MARK16.Checked = false;
            BIT_MARK17.Checked = false;
            BIT_MARK18.Checked = false;
            BIT_MARK19.Checked = false;
            BIT_MARK20.Checked = false;
            BIT_MARK21.Checked = false;
            BIT_MARK22.Checked = false;
            BIT_MARK23.Checked = false;
            BIT_MARK24.Checked = false;
            BIT_MARK25.Checked = false;
            BIT_MARK26.Checked = false;
            BIT_MARK27.Checked = false;
            BIT_MARK28.Checked = false;
            BIT_MARK29.Checked = false;
            BIT_MARK30.Checked = false;
            BIT_MARK31.Checked = false;
        }
        private void BIT_MARK_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                contextMenuStrip3.Show(Control.MousePosition.X, Control.MousePosition.Y);
                if ((CheckBox)sender == BIT_MARK0) care_bit_index = 0;
                else if ((CheckBox)sender == BIT_MARK1) care_bit_index = 1;
                else if ((CheckBox)sender == BIT_MARK2) care_bit_index = 2;
                else if ((CheckBox)sender == BIT_MARK3) care_bit_index = 3;
                else if ((CheckBox)sender == BIT_MARK4) care_bit_index = 4;
                else if ((CheckBox)sender == BIT_MARK5) care_bit_index = 5;
                else if ((CheckBox)sender == BIT_MARK6) care_bit_index = 6;
                else if ((CheckBox)sender == BIT_MARK7) care_bit_index = 7;
                else if ((CheckBox)sender == BIT_MARK8) care_bit_index = 8;
                else if ((CheckBox)sender == BIT_MARK9) care_bit_index = 9;
                else if ((CheckBox)sender == BIT_MARK10) care_bit_index = 10;
                else if ((CheckBox)sender == BIT_MARK11) care_bit_index = 11;
                else if ((CheckBox)sender == BIT_MARK12) care_bit_index = 12;
                else if ((CheckBox)sender == BIT_MARK13) care_bit_index = 13;
                else if ((CheckBox)sender == BIT_MARK14) care_bit_index = 14;
                else if ((CheckBox)sender == BIT_MARK15) care_bit_index = 15;
                else if ((CheckBox)sender == BIT_MARK16) care_bit_index = 16;
                else if ((CheckBox)sender == BIT_MARK17) care_bit_index = 17;
                else if ((CheckBox)sender == BIT_MARK18) care_bit_index = 18;
                else if ((CheckBox)sender == BIT_MARK19) care_bit_index = 19;
                else if ((CheckBox)sender == BIT_MARK20) care_bit_index = 20;
                else if ((CheckBox)sender == BIT_MARK21) care_bit_index = 21;
                else if ((CheckBox)sender == BIT_MARK22) care_bit_index = 22;
                else if ((CheckBox)sender == BIT_MARK23) care_bit_index = 23;
                else if ((CheckBox)sender == BIT_MARK24) care_bit_index = 24;
                else if ((CheckBox)sender == BIT_MARK25) care_bit_index = 25;
                else if ((CheckBox)sender == BIT_MARK26) care_bit_index = 26;
                else if ((CheckBox)sender == BIT_MARK27) care_bit_index = 27;
                else if ((CheckBox)sender == BIT_MARK28) care_bit_index = 28;
                else if ((CheckBox)sender == BIT_MARK29) care_bit_index = 29;
                else if ((CheckBox)sender == BIT_MARK30) care_bit_index = 30;
                else if ((CheckBox)sender == BIT_MARK31) care_bit_index = 31;

            }
        }
        private void contextMenuStrip3_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            for (int i = 0; i < contextMenuStrip3.Items.Count; i++)
            {
                if (contextMenuStrip3.Items[i].Selected)
                {
                    if (contextMenuStrip3.Items[i].Text.Trim() == "定位最低Bit")
                    {
                        if(care_bit_index>= care_bit_high)
                        {
                            MessageBox.Show("设置错误,底位必须底于高位");
                            return;
                        }
                        if ((care_bit_high-care_bit_index) > 19)
                        {
                            MessageBox.Show("设置错误,高位和底位差值不得大于15");
                            return;
                        }
                        care_bit_low = care_bit_index;
                        label57.Text = "[" + care_bit_high.ToString("D2") + ":" + care_bit_low.ToString("D2") + "]";

                    }
                    else if (contextMenuStrip3.Items[i].Text.Trim() == "定位最高Bit")
                    {
                        if(care_bit_index<= care_bit_low)
                        {
                            MessageBox.Show("设置错误,高位必须高于底位");
                            return;
                        }
                        if ((care_bit_index - care_bit_low)>19)
                        {
                            MessageBox.Show("设置错误,高位和底位差值不得大于15");
                            return;
                        }
                        care_bit_high = care_bit_index;
                        label57.Text = "[" + care_bit_high.ToString("D2") + ":" + care_bit_low.ToString("D2") + "]";
                    }
                    else if (contextMenuStrip3.Items[i].Text.Trim() == "定位第二组最低Bit")
                    {
                        if (care_bit_index >= care_bit_high_two)
                        {
                            MessageBox.Show("设置错误,底位必须底于高位");
                            return;
                        }
                        care_bit_low_two = care_bit_index;
                        label11.Text = "[" + care_bit_high_two.ToString("D2") + ":" + care_bit_low_two.ToString("D2") + "]";

                    }
                    else if (contextMenuStrip3.Items[i].Text.Trim() == "定位第二组最高Bit")
                    {
                        if (care_bit_index <= care_bit_low_two)
                        {
                            MessageBox.Show("设置错误,高位必须高于底位");
                            return;
                        }
                        care_bit_high_two = care_bit_index;
                        label11.Text = "[" + care_bit_high_two.ToString("D2") + ":" + care_bit_low_two.ToString("D2") + "]";
                    }
                    if ((care_bit_high - care_bit_low) < 19) Care_BIT_MARK19.Visible = false;
                    else Care_BIT_MARK19.Visible = true;
                    if ((care_bit_high - care_bit_low) < 18) Care_BIT_MARK18.Visible = false;
                    else Care_BIT_MARK18.Visible = true;
                    if ((care_bit_high - care_bit_low) < 17) Care_BIT_MARK17.Visible = false;
                    else Care_BIT_MARK17.Visible = true;
                    if ((care_bit_high - care_bit_low) < 16) Care_BIT_MARK16.Visible = false;
                    else Care_BIT_MARK16.Visible = true;
                    if ((care_bit_high - care_bit_low) < 15) Care_BIT_MARK15.Visible = false;
                    else Care_BIT_MARK15.Visible = true;
                    if ((care_bit_high - care_bit_low) < 14) Care_BIT_MARK14.Visible = false;
                    else Care_BIT_MARK14.Visible = true;
                    if ((care_bit_high - care_bit_low) < 13) Care_BIT_MARK13.Visible = false;
                    else Care_BIT_MARK13.Visible = true;
                    if ((care_bit_high - care_bit_low) < 12) Care_BIT_MARK12.Visible = false;
                    else Care_BIT_MARK12.Visible = true;
                    if ((care_bit_high - care_bit_low) < 11) Care_BIT_MARK11.Visible = false;
                    else Care_BIT_MARK11.Visible = true;
                    if ((care_bit_high - care_bit_low) < 10) Care_BIT_MARK10.Visible = false;
                    else Care_BIT_MARK10.Visible = true;
                    if ((care_bit_high - care_bit_low) < 9) Care_BIT_MARK9.Visible = false;
                    else Care_BIT_MARK9.Visible = true;
                    if ((care_bit_high - care_bit_low) < 8) Care_BIT_MARK8.Visible = false;
                    else Care_BIT_MARK8.Visible = true;
                    if ((care_bit_high - care_bit_low) < 7) Care_BIT_MARK7.Visible = false;
                    else Care_BIT_MARK7.Visible = true;
                    if ((care_bit_high - care_bit_low) < 6) Care_BIT_MARK6.Visible = false;
                    else Care_BIT_MARK6.Visible = true;
                    if ((care_bit_high - care_bit_low) < 5) Care_BIT_MARK5.Visible = false;
                    else Care_BIT_MARK5.Visible = true;
                    if ((care_bit_high - care_bit_low) < 4) Care_BIT_MARK4.Visible = false;
                    else Care_BIT_MARK4.Visible = true;
                    if ((care_bit_high - care_bit_low) < 3) Care_BIT_MARK3.Visible = false;
                    else Care_BIT_MARK3.Visible = true;
                    if ((care_bit_high - care_bit_low) < 2) Care_BIT_MARK2.Visible = false;
                    else Care_BIT_MARK2.Visible = true;
                    if ((care_bit_high - care_bit_low) < 1) Care_BIT_MARK1.Visible = false;
                    else Care_BIT_MARK1.Visible = true;
                }
            }
        }
        private void button10_Click(object sender, EventArgs e)
        {
            if (extend_bit_mask_state == false)
            {
                this.Height -= 375;
                extend_bit_mask_state = true;
            }
            else
            {
                this.Height += 375;
                extend_bit_mask_state = false;
            }
        }

        private void BIT_MARK_CheckStateChanged(object sender, EventArgs e)
        {
            bitmask_checkbox_Click(sender, e);
        }
        private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                //if ((TextBox)sender == textBoxR)
                //    this.textBoxG.Focus();
            }
            else if (e.KeyChar == '\x1')
            {
                ((TextBox)sender).SelectAll();
                e.Handled = true;
            }
        }
        private void turn_text_color(object sender)//翻转字体颜色，让使用者更容易知道该值改变了
        {
            if (sender is TextBox)
            {
                if (((TextBox)sender).ForeColor == Color.Red)
                {
                    ((TextBox)sender).ForeColor = Color.Blue;
                }
                else
                {
                    ((TextBox)sender).ForeColor = Color.Red;
                }
            }
        }
        private void recover_text_color(object sender)//恢复字体颜色，说明本操作不会修改该值
        {
            if (sender is TextBox)
            {
                ((TextBox)sender).ForeColor = Color.Black;
            }
        }
        private void button34_Click(object sender, EventArgs e)
        {
            double x0, y0, x1, y1, k;
            x0 = Convert.ToDouble(textBox_input_X0.Text);
            y0 = Convert.ToDouble(textBox_input_Y0.Text);
            x1 = Convert.ToDouble(textBox_input_X1.Text);
            if (comboBox_piecwise_mode.SelectedIndex == 0)
            {
                y1 = Convert.ToDouble(textBox_input_Y1.Text);
                k = (y1 - y0) / (x1 - x0);
                textBox_output_formula.Text = "y=(" + y1.ToString() + ")+(" + k.ToString("f3") + ")*x -(" + (k * x1).ToString("f3") + ");";
                turn_text_color(textBox_output_formula);
                recover_text_color(textBox_input_Y1);
            }
            else if (comboBox_piecwise_mode.SelectedIndex == 1)
            {
                y1 = (x1 * y0) / x0;
                textBox_input_Y1.Text = y1.ToString("f3");
                turn_text_color(textBox_input_Y1);
                recover_text_color(textBox_output_formula);
            }
            else if (comboBox_piecwise_mode.SelectedIndex == 2)
            {
                y1 = y0 / (x0+ y0) * x1;
                textBox_input_Y1.Text = y1.ToString("f3");
                turn_text_color(textBox_input_Y1);
            }
        }
        private void comboBox_piecwise_mode_DropDownClosed(object sender, EventArgs e)
        {
            if (comboBox_piecwise_mode.SelectedIndex == 2)
            {
                this.label106.Text = "RU(上拉):";
                this.label107.Text = "VT:";
                this.label104.Text = "RD(下拉):";
                this.label105.Text = "V:";
            }
            else
            {
                this.label106.Text = "X0(输入):";
                this.label107.Text = "X1:";
                this.label104.Text = "Y0(输出):";
                this.label105.Text = "Y1:";
            }
        }
        private void button7_Click(object sender, EventArgs e)
        {
            double x0, y0, x1, y1;
            x0 = Convert.ToDouble(textBox_input_X0.Text);
            y0 = Convert.ToDouble(textBox_input_Y0.Text);
            x1 = Convert.ToDouble(textBox_input_X1.Text);
            y1 = Convert.ToDouble(textBox_input_Y1.Text);
            x0 = 1 / x0;
            y0 = 1 / y0;
            x1 = 1 / x1;
            y1 = 1 / y1;
            textBox_input_X0.Text = x0.ToString("f3");
            textBox_input_X1.Text = x1.ToString("f3");
            textBox_input_Y0.Text = y0.ToString("f3");
            textBox_input_Y1.Text = y1.ToString("f3");
        }
        private void button29_Click(object sender, EventArgs e)
        {
            string x0 = textBox_input_X0.Text;
            string x1 = textBox_input_X1.Text;
            textBox_input_X1.Text = textBox_input_Y1.Text;
            textBox_input_X0.Text = textBox_input_Y0.Text;

            textBox_input_Y1.Text = x1;
            textBox_input_Y0.Text = x0;
        }

        private void textBoxTrim_Leave(object sender, EventArgs e)
        {
            ((TextBox)sender).Text = ((TextBox)sender).Text.Trim();
        }

        private void textBoxRemoveBlack_Leave(object sender, EventArgs e)
        {
            ((TextBox)sender).Text = ((TextBox)sender).Text.Replace(" ","");
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.L && e.Control)
            {
            }
        }
        private void TabCrontrol_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (TabCrontrol.SelectedIndex <= 4)
            {
                pictureBoxinterface_Doing(true);
            }
            else
            {
                pictureBoxinterface_Doing(false);
            }
            extend_bit_mask_state = false;
            timer_cal_mask_state = false;
        }
        public void textBox_HexStringToString(TextBox textBox)
        {
            textBox.Text = HexStringToString(textBox.Text);
        }
        public void textBox_StringToHexString(TextBox textBox)
        {
            textBox.Text = StringToHexString(textBox.Text, "X8");
        }
        private void timestamp_Difference_hex_CheckedChanged(object sender, EventArgs e)
        {
            if (timestamp_Difference_hex.Checked == false)
            {
                textBox_HexStringToString(textBox37);
                textBox_HexStringToString(textBox36);
                textBox_HexStringToString(textBox33);
                textBox_HexStringToString(textBox32);
            }
            else
            {
                textBox_StringToHexString(textBox37);
                textBox_StringToHexString(textBox36);
                textBox_StringToHexString(textBox33);
                textBox_StringToHexString(textBox32);
            }
        }

        private void timestamp_Difference_select_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool display_befor = true;
            if (timestamp_Difference_select.SelectedIndex == 0)
            {
                accuracy = timestamp_accuracy_type.accuracy_3;
            }
            else if (timestamp_Difference_select.SelectedIndex == 1)
            {
                accuracy = timestamp_accuracy_type.accuracy_6;
            }
            else if (timestamp_Difference_select.SelectedIndex == 2)
            {
                accuracy = timestamp_accuracy_type.Display_DATA;
            }
            else if (timestamp_Difference_select.SelectedIndex == 3)
            {
                accuracy = timestamp_accuracy_type.CONVERT_UTC8;
                display_befor = false;
            }
            else if (timestamp_Difference_select.SelectedIndex == 4)
            {
                accuracy = timestamp_accuracy_type.CONVERT_UTC;
                display_befor = false;
            }
            if (display_befor)
            {
                textBox36.Enabled = true;
                textBox32.Enabled = true;
            }
            else
            {
                textBox36.Enabled = false;
                textBox32.Enabled = false;
            }
        }

        private void textBox48_TextChanged(object sender, EventArgs e)
        {
            if (textBox48.Text.Trim().Length == 0) return;

            Double p = 0.12345;
            switch (comboBox6.SelectedIndex)
            {
                case 0:
                    p = (double)1 / 4;
                    break;
                case 1:
                    p = (double)1 / 8;
                    break;
                case 2:
                    p = (double)1 / 10;
                    break;
                case 3:
                    p = (double)1 / 16;
                    break;
                case 4:
                    p = (double)1 / 20;
                    break;
            }
            Double r = Convert.ToDouble(textBox48.Text);
            if (r == 0) return;
            p = p / r;
            p = Math.Sqrt(p);
            textBox46.Text= p.ToString("f5");
        }

        private void textBox_rename_TextChanged(object sender, EventArgs e)
        {
            checkBox_delete_srcfile.Checked = false;
            checkBox_delete_prefile.Checked = false;
            checkBox16.Checked = false;
        }
        private void cal_clear_click_adjust(ComboBox combobox, TextBox textbox)
        {
            if ((combobox.SelectedIndex == 2) || (combobox.SelectedIndex == 3) || (combobox.SelectedIndex == 4))
                textbox.Text = "1";
            else
                textbox.Text = "0";
        }
        private void label4_Click(object sender, EventArgs e)
        {
            //EventArgs继承自MouseEventArgs,所以可以强转
            MouseEventArgs Mouse_e = (MouseEventArgs)e;
            //点鼠标右键,return
            if (Mouse_e.Button == MouseButtons.Right)
            {
                textBox6.Text = "0";
                textBox3.Text = "0";
                textBox43.Text = "0";
                cal_clear_click_adjust(comboBox3, textBox1);
                cal_clear_click_adjust(comboBox2, textBox8);
                cal_clear_click_adjust(comboBox4, textBox2);
                cal_clear_click_adjust(comboBox1, textBox5);
                cal_clear_click_adjust(comboBox5, textBox11);
                cal_clear_click_adjust(comboBox8, textBox45);
                cal_clear_click_adjust(comboBox10, textBox49);
                cal_clear_click_adjust(comboBox11, textBox50);
                cal_clear_click_adjust(comboBox9, textBox47);
                cal_clear_click_adjust(comboBox13, textBox52);
                cal_clear_click_adjust(comboBox14, textBox53);
                cal_clear_click_adjust(comboBox12, textBox51);
            }
            else
            {
                if (timer_cal_mask_state == false)
                {
                    this.Height -= 475;
                    timer_cal_mask_state = true;
                }
                else
                {
                    this.Height += 475;
                    timer_cal_mask_state = false;
                }
            }
        }

        public void StripStatusLabelSet(string text)
        {
            toolStripStatusLabel2.Text = text;
            if (this.toolStripStatusLabel2.ForeColor == Color.Red)
            {
                this.toolStripStatusLabel2.ForeColor = Color.Blue;
            }
            else
            {
                this.toolStripStatusLabel2.ForeColor = Color.Red;
            }
        }
        public void StripStatusLabelEnd(string text)
        {
            if (this.toolStripStatusLabel2.ForeColor == Color.Red)
            {
                toolStripStatusLabel2.Text = text;
                this.toolStripStatusLabel2.ForeColor = Color.Blue;
            }
        }
    }
}
